layui.use(['element', 'layer', 'form', 'element', 'laydate', 'zlConfig', 'zlContext', 'upload'], function (exports) {
    var $ = layui.$
        , element = layui.element
        , zlContext = layui.zlContext
        , zlConfig = layui.zlConfig
        , statusName = zlConfig.global.result.statusName
        , msgName = zlConfig.global.result.msgName
        , dataName = zlConfig.global.result.dataName
        , OK = zlConfig.global.result.okState

    var index = layer.load();
    getData();
    function getData() {
        //获取磁盘
        getDiscData();
        //获取CPU和内存
        getCandMData();
    }

    function getCandMData() {
        zlContext.post(layui.cache['contentPath'] + '/manager/server/query', {}, function (result) {
            if (result[statusName] == OK) {
                var datas = result[dataName];
                render(datas);
            } else {
                //不展示错误
                // zlContext.errorMsg(result[msgName]);
            }
            layer.close(index);
        }, false);
    }

    function render(data) {
        if (data) {
            var memory = data.memory,
                cpu = data.cpu;
            if (Number(cpu.replace("%", "")) > 80) {
                $('#cpu').addClass('layui-bg-bigred');
            } else {
                $('#cpu').removeClass('layui-bg-bigred');
            }
            if (Number(memory.replace("%", "")) > 80) {
                $('#memory').addClass('layui-bg-bigred');
            } else {
                $('#memory').removeClass('layui-bg-bigred');
            }
            $('#memory').attr("desc", "已使用" + memory + "的内存");
            $('#cpu').attr("desc", "已使用" + cpu + "的CPU");
            element.progress('cpu', cpu);
            element.progress('memory', memory);
        }
    }
    setInterval(function () {
        getCandMData();
    }, 5000 * 12 * 5);

    function getDiscData() {
        zlContext.post(layui.cache['contentPath'] + '/manager/server/querydisc', {}, function (result) {
            if (result[statusName] == OK) {
                var datas = result[dataName];
                renderDisc(datas);
            } else {
                zlContext.errorMsg(result[msgName]);
            }
            layer.close(index);
        }, false);
    }

    function renderDisc(datas) {
        for (var i = 0; i < datas.length; i++) {
            var data = datas[i],
                html = [
                    , '<div class="layui-progress layui-progress-big" lay-showpercent="yes" >'
                    , '    <h3>' + data.name + '</h3>'
                    , '    <div desc="' + data.desc + '" class="layui-progress-bar' + (Number(data.value.replace("%", "")) > 80 ? ' layui-bg-bigred"' : '"') + '" lay-percent="' + data.value + '">'
                    , '    </div>'
                    , '</div'
                ];
            $('.layui-card-body').append(html.join(''));
            //重新渲染
            element.render('progress');
        }
        $(".layui-progress-bar").mouseover(function () {
            var desc = $(this).attr("desc");
            if (desc) {
                layer.tips(desc, this, {
                    tips: [3, $(this).css('background-color')]
                });
            }
        });
        setInterval(litSlash, 500);

    }

    function litSlash() {
        $(".layui-bg-bigred").animate({ opacity: "0" }, 250); //改变不透明度
        $(".layui-bg-bigred").animate({ opacity: "1" }, 50);
    }

});